package com.opal.jx.service.impl;

import com.opal.consts.WSC;
import com.opal.domain.R;
import com.opal.jx.JxConst.JxConst;
import com.opal.jx.entity.JxAssessReception;
import com.opal.jx.entity.table.*;
import com.opal.jx.exception.JxAssessException;
import com.opal.jx.mapper.mysql.JxAssessBzcjFzryMapper;
import com.opal.jx.mapper.mysql.JxAssessBzcjMapper;
import com.opal.jx.service.*;
import com.opal.model.easyui.DataGridColumn;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Slf4j
public class JxBZCJServiceImpl implements JxBZCJService {

//    @Autowired
//    private OpalDictService opalDicts;    //词典
    @Autowired
    private JxAssessBzcjMapper jxAssessBzcjMapper;

    @Autowired
    private JxEmpService jxEmpService;

    @Autowired
    @Qualifier(value = "jxAssessBzcjTotalServiceImpl") //有相同接口实现类指定注入名称为jxAssessBzcjTotalService
    private JxAssessTotalService jxAssessBzcjTotalService;
    @Autowired
    private JxAssessCommonService jxAssessCommonService;

    @Autowired
    private JxAssessBzcjFzryMapper jxAssessBzcjFzryMapper;    //包装车间--辅助人员

    @Autowired
    private JxAssessBzcjZgService jxAssessBzcjZgService;    //包装车间--主管

    @Override
    @Transactional("mysqlTransactionManager")
    public R addAssessDataService(JxAssessReception jxAssessReception) {
        isSubmitData(jxAssessReception);
        //获取绩效数据
        List<JxAssessBzcj>  jxAssessBzcjs = jxAssessReception.getJxAssessBzcjs();
        JxAssessBzcj jxAssessBzcj = jxAssessBzcjs.get(0);
        Integer assessType = jxAssessBzcj.getAssessType();
        //获取月脚行的合计数据
        List<Map<String, String>> maps = jxAssessReception.getMaps();
        switch (assessType){
            case 1:  //一线员工
                addYiXianYuanGongData(jxAssessBzcj,maps,jxAssessBzcjs);
                break;
            case 2: //组长
             addZuZhangData(jxAssessBzcj,maps,jxAssessBzcjs);
        }

        return new R(WSC.RESTPONSE_OK,JxConst.SUBMIT_SUCCESS);
    }

    //添加组长绩效数据
    private void addZuZhangData(JxAssessBzcj jxAssessBzcj, List<Map<String, String>> maps, List<JxAssessBzcj> jxAssessBzcjs) {
        Integer assessType = jxAssessBzcj.getAssessType();
        Integer year = jxAssessBzcj.getAssessYear();
        Integer month = jxAssessBzcj.getAssessMonth();
        String empGroup = jxAssessBzcj.getEmpGroup();
        //先清空当月数据避免重复：年、月、类型数据
        deleteAssessData(year,month,null,assessType);

        //添加数据
        addBZCJData(maps, jxAssessBzcjs, year, month, empGroup,assessType);

    }

    //添加一线员工绩效数据
    private void addYiXianYuanGongData(JxAssessBzcj jxAssessBzcj, List<Map<String, String>> maps, List<JxAssessBzcj> jxAssessBzcjs) {

        Integer assessType = jxAssessBzcj.getAssessType();
        Integer year = jxAssessBzcj.getAssessYear();
        Integer month = jxAssessBzcj.getAssessMonth();
        String empGroup = jxAssessBzcj.getEmpGroup();           //班组

        //先清空当月数据避免重复：年、月、一线员工类型数据
        deleteAssessData(year,month,empGroup,assessType);
        //添加数据
        addBZCJData(maps, jxAssessBzcjs, year, month, empGroup,assessType);
    }

    private void addBZCJData(List<Map<String, String>> maps, List<JxAssessBzcj> jxAssessBzcjs, Integer year, Integer month, String empGroup,Integer assessType) {
        jxAssessBzcjs.stream().forEach(a->{
            String resultMoney = a.getResultMoney();
            if (StringUtils.isBlank(resultMoney)){
                throw new JxAssessException(WSC.PRECONDITIONFAILED, JxConst.SUBMIT_EFEATED_RWAULTMONEY_NULL_DATA);
            }else if(resultMoney.equals("NaN")){
                throw new JxAssessException(WSC.PRECONDITIONFAILED, JxConst.SUBMIT_EFEATED_NULL_DATA);
            }
            //插入数据
            jxAssessBzcjMapper.insertSelective(a);
        });
        //插入合计
        addTotal(maps, year, month, empGroup, assessType);

    }

    private void addTotal(List<Map<String, String>> maps, Integer year, Integer month, String empGroup, Integer assessType) {
        //避免重复--先清空当月类型的数据
        JxAssessTotal jt= new JxAssessTotal();
        jt.setAssessType(assessType);                       //绩效数据类型：1、一线员工，2、组长，3、辅助人员，4、主管
        jt.setAssessMonth(month);                           //年份
        jt.setAssessYear(year);                             //月份
        if (empGroup !=null) {
            jt.setEmpGroup(empGroup);                         //班组
        }
        jxAssessBzcjTotalService.deleteByExample(jt);

        //添加合计表：jx_assess_total
        for (Map<String,String> map:maps){
            String resultMoney = map.get("resultMoney");
            String name = map.get("empCode");
            //总金额为null
            if (StringUtils.isBlank(resultMoney)){
                throw new JxAssessException(WSC.REQUEST_ERROR, JxConst.SUBMIT_EFEATED_RESULTMONEY_NULL);
            }
            jt.setTotalMoney(resultMoney);                              //班组总绩效奖金
            jt.setRemarks(name);                            // 备注
            jxAssessBzcjTotalService.insertSelective(jt);
        }
    }

    //根据年、月、绩效数据类型删除数据
    private void deleteAssessData(Integer year, Integer month,String empGrop, Integer assessType) {

            //3个必须
            if (year ==null){
                throw new JxAssessException(WSC.REQUEST_ERROR,JxConst.SUBMIT_EFEATED_YEAR_NULL);
            }
            if (month ==null){
                throw new JxAssessException(WSC.REQUEST_ERROR,JxConst.SUBMIT_EFEATED_MONTH_NULL);
            }
            if (assessType ==null){
                throw new JxAssessException(WSC.REQUEST_ERROR,JxConst.SUBMIT_EFEATED_ASSESSTYPE_NULL);
            }
            JxAssessBzcjExample jxAssessBzcjExample=new JxAssessBzcjExample();

            JxAssessBzcjExample.Criteria criteria = jxAssessBzcjExample.createCriteria().andAssessYearEqualTo(year).andAssessMonthEqualTo(month).andAssessTypeEqualTo(assessType);
            //班组若有值，加上班组条件
            if (!StringUtils.isBlank(empGrop)){
                criteria.andEmpGroupEqualTo(empGrop);
            }
            //删除该类型的数据
            jxAssessBzcjMapper.deleteByExample(jxAssessBzcjExample);
            //删除该类型的总合计数据
            JxAssessTotal jxAssessTotal = new JxAssessTotal();
            jxAssessTotal.setAssessType(assessType);
            jxAssessTotal.setEmpGroup(empGrop);
            jxAssessTotal.setAssessYear(year);
            jxAssessTotal.setAssessMonth(month);
        jxAssessBzcjTotalService.deleteByExample(jxAssessTotal);
    }

    @Override
    public List<JxAssessBzcj> selectJxAssessBzcjByCondition(JxAssessBzcj condition) {
        List<JxAssessBzcj> jxAssessBzcjs = jxAssessBzcjMapper.selectByExample(getJxAssessBzcjExample(condition));
        return jxAssessBzcjs;
    }

    //查询绩效数据
    @Override
    public List<JxAssessBzcj>  selectJxAssessBzcjService(JxAssessReception jxAssessReception) {
        String pkDept = jxAssessReception.getPkDept();
        Integer assessType = jxAssessReception.getAssessType();
        String empGroup = jxAssessReception.getEmpGroup();
        String postId = jxAssessReception.getPostId();
        String empZuzhang = jxAssessReception.getEmpZuzhang(); //若有值证明是要查询组长数据


        //根据：年、月、部门、绩效类型、组别，查询是否有绩效数据
        JxAssessBzcj jxAssessBzcj = new JxAssessBzcj();
        jxAssessBzcj.setAssessType(assessType);
        jxAssessBzcj.setAssessMonth(jxAssessReception.getMonth());
        jxAssessBzcj.setAssessYear(jxAssessReception.getYear());
        //jxAssessBzcj.setAssessDept(pkDept);
        //一线员工才需要条件分组查询
        if (empZuzhang==null){
            jxAssessBzcj.setEmpGroup(empGroup);
        }
         //查询数据
        List<JxAssessBzcj> jxAssessBzcjs = this.selectJxAssessBzcjByCondition(jxAssessBzcj);

        //从员工表中获取
        if (jxAssessBzcjs.size()<=0){
             //根据条件：组别(组长id)获取当前组别的人员数据
            JxEmp jxEmp = new JxEmp();
            jxEmp.setPkDept(pkDept);
            List<JxEmp> jxEmps;
            //处理组长、主管的数据
            if (empZuzhang==null){
                //一线员工：，不需要主管的名称的数据
                jxEmp.setEmpZuzhang(empGroup);                   //一线员工组别：以组长工号辨别
                //根据组长(组长工号)、部门获取到一线员工信息
                jxEmps = jxEmpService.selectJxEmpByCondition(jxEmp);

                //封装数据：工号、姓名、所属组长名字、
                for (JxEmp a:jxEmps){
                    JxAssessBzcj jb= new JxAssessBzcj();
                    jb.setEmpName(a.getEmpName());  //姓名
                    jb.setEmpCode(a.getEmpCode());  //工号
                    jb.setZuZhangName(jxAssessReception.getPostName()); //所属组长名称
                    jb.setEmpGroup(empGroup);       //班组
                    jxAssessBzcjs.add(jb);
                }
            }else {
                jxAssessBzcjs= jxAssessCommonService.jxAssessCommonMapper(postId,pkDept);
            }

        }
         return jxAssessBzcjs;
    }

    @Override
    public Object selectBZCJAllGroupLeader(JxAssessReception jxAssessReception) {
        JxEmp jxEmp = new JxEmp();
        jxEmp.setPostId(Long.parseLong(jxAssessReception.getPostId()));
        jxEmp.setPkDept(jxAssessReception.getPkDept());
        return jxEmpService.selectJxEmpByCondition(jxEmp);
    }

    @Override
    public Object selectBzcjFuZhuRenYuanService(JxAssessReception jxAssessReception) {
        Integer month = jxAssessReception.getMonth();
        Integer year = jxAssessReception.getYear();
        String pkDept = jxAssessReception.getPkDept();
        Map<String,Object> map = new HashedMap();
        List<JxAssessBzcjFzry> fuZhuRenYuan = new ArrayList<>();
        List<JxAssessBzcjFzry> fuZhuZuZhang = new ArrayList<>();
        List<JxAssessBzcjFzry> fuZhuZaGong = new ArrayList<>();
        //查看当月绩效表jxAssess_bzcj_fzry中是否有数据
       List<JxAssessBzcjFzry> jxAssessBzcjFzrys= jxAssessBzcjFzryMapper.selectBzcjFuZhuRenYuan(year,month);
       if (jxAssessBzcjFzrys.size()>0){
           jxAssessBzcjFzrys.forEach(a->{
               Integer type = a.getAssessType();
               // 类型：1、辅助人员；2、辅助组长；3、搬运工、清洁工
               if (type==1){
                   fuZhuRenYuan.add(a);
               }
               if (type ==2){
                   fuZhuZuZhang.add(a);
               }
              if (type==3){
                  fuZhuZaGong.add(a);
              }
           });

       }else {
            //获取前端传送过来的{"辅助主管"：17，.....}
           List<Map<String, String>> maps = jxAssessReception.getMaps();
           Map<String, String> post = maps.get(0);
           Long zuGuanPostId = null;    //辅助主管 岗位id
           Long zuZhangPostId = null;    //辅助组长 岗位id
           Long renYuanPostId = null;    //辅助人员 岗位id

           for (String m :post.keySet()){
             Long postId=  Long.parseLong(post.get(m));
               if (m.contains("主管")){
                   zuGuanPostId=postId;
               }else if (m.contains("组长")){
                   zuZhangPostId=postId;
               }else {
                   renYuanPostId=postId;
               }
           }

           //首先查询辅助主管
           //select * from jx_emp where post_id='17' and pk_dept = '1002O110000000000MMQ'

           JxEmp jxEmp= new JxEmp();
           jxEmp.setPostId(zuGuanPostId);
           jxEmp.setPkDept(pkDept);
           //查询辅助主管：通过职位id和部门
           List<JxEmp> jxEmps = jxEmpService.selectJxEmpByCondition(jxEmp);
           for (JxEmp j:jxEmps){
               String empCode = j.getEmpCode(); //辅助主管工号
               //1、查询辅助组长：通过辅助主管工号查询：条件辅助主管工号和辅助组长的岗位id
              List<JxEmp> zuZhangs= jxEmpService.selectJxEmpByEmpZhuGuan(empCode,zuZhangPostId);

              for (JxEmp z:zuZhangs){
                  String zuZhangCode = z.getEmpCode(); //辅助组长工号
                  JxAssessBzcjFzry jxAssessZuZhang = packagingJxAssessBzcjFzryData(z);
                  jxAssessZuZhang.setZhuGuanName(j.getEmpName());
                  jxAssessZuZhang.setZuZhangName(z.getEmpName());
                  fuZhuZuZhang.add(jxAssessZuZhang);
                  //2、查询辅助人员：通过辅助组长工号查询
                  List<JxEmp> fuZhuRenYuans = jxEmpService.selectJxEmpByEmpZuZhang(zuZhangCode,renYuanPostId);
                  for (JxEmp fz: fuZhuRenYuans){
                      JxAssessBzcjFzry fzry = packagingJxAssessBzcjFzryData(fz);
                      fzry.setZhuGuanName(j.getEmpName());
                      fzry.setZuZhangName(z.getEmpName());
                      fuZhuRenYuan.add(fzry);
                  }
              }
           }

           //3、搬运工、清洁工等
           //#根据主管工号找辅助杂工(搬运工、清洁工)，写个通用的:1、获取当前部门的所有辅助主管，2、然后去除辅助主管、辅助组长、辅助人员，剩下来的就是杂工
           //select * from jx_emp where emp_zhuguan in (select emp_code from jx_emp where post_id='17' and pk_dept = '1002O110000000000MMQ') and post_id not in('17','14','13')
           List<String> ps= new ArrayList<>();
           for (String m :post.values()){
               ps.add(m);
           }
           List<JxEmp> zaGongs= jxAssessCommonService.selectFuZhuZaGongInfo(zuGuanPostId,pkDept,ps);
           for (JxEmp fz: zaGongs){
               //封装数据
               JxAssessBzcjFzry fzry = packagingJxAssessBzcjFzryData(fz);
               JxEmp je= new JxEmp();
               je.setEmpCode(fz.getEmpZhuguan());//根据所属主管工号获取主管工号
               //查询主管员工信息：根据工号查询
               List<JxEmp> zhuGuan = jxEmpService.selectJxEmpByCondition(je);
               if (zhuGuan.size()>0){
                   //封装主管姓名
                   fzry.setZhuGuanName(zhuGuan.get(0).getEmpName());
               }
               fuZhuZaGong.add(fzry);
           }
       }
        //和前端约定的名称
        map.put("fuZhuRenYuan",fuZhuRenYuan);
        map.put("fuZhuZuZhang",fuZhuZuZhang);
        map.put("fuZhuZaGong",fuZhuZaGong);
        return map;
    }

    private  JxAssessBzcjFzry packagingJxAssessBzcjFzryData(JxEmp jxEmp){
        JxAssessBzcjFzry jxAssessBzcjFzry= new JxAssessBzcjFzry();
        String empCode = jxEmp.getEmpCode();
        String empName = jxEmp.getEmpName();
        jxAssessBzcjFzry.setEmpName(empName);
        jxAssessBzcjFzry.setEmpCode(empCode);
        return jxAssessBzcjFzry;
    }

    @Override
    @Transactional("mysqlTransactionManager")
    public Object addBzcjFuZhuRenYuanService(JxAssessReception jxAssessReception) {
        //总合计
        List<Map<String, String>> maps = jxAssessReception.getMaps();
        //包装车间--辅助人员数据
        List<JxAssessBzcjFzry> jxAssessBzcjFzrys = jxAssessReception.getJxAssessBzcjFzrys();
        JxAssessBzcjFzry jxAssessBzcjFzry = jxAssessBzcjFzrys.get(0);
        Integer assessYear = jxAssessBzcjFzry.getAssessYear();
        Integer assessMonth = jxAssessBzcjFzry.getAssessMonth();
        //避免重复--先清空当月数据
        jxAssessBzcjFzryMapper.deleteByYearAndMonth(assessYear,assessMonth);

        jxAssessBzcjFzrys.forEach(v -> {
            String resultMoney = v.getResultMoney();
            if (StringUtils.isBlank(resultMoney)){
                throw new JxAssessException(WSC.PRECONDITIONFAILED, JxConst.SUBMIT_EFEATED_RWAULTMONEY_NULL_DATA);
            }else if(resultMoney.equals("NaN")){
                throw new JxAssessException(WSC.PRECONDITIONFAILED, JxConst.SUBMIT_EFEATED_NULL_DATA);
            }
            //插入数据
            jxAssessBzcjFzryMapper.insert(v);
        });
        //插入总合计
        addTotal(maps,assessYear,assessMonth,null,3);//绩效数据类型：1、一线员工，2、组长，3、辅助人员，4、主管

        return new R(WSC.RESTPONSE_OK,JxConst.SUBMIT_SUCCESS);
    }

    @Override
    public Object getBzcjZhuGuanJxAssessService(Integer year,Integer month) {
        List resultData=new ArrayList();
       List<JxAssessBzcjZg> jx= jxAssessCommonService.getCxAndFzAssessDataByYearAndMonth(year,month);
       for (JxAssessBzcjZg j:jx){
           Map<String,Object> maps= new HashedMap();
           List listData=new ArrayList();
           listData.add(j);
           //封装数据
           maps.put("data",listData);
           //封装字段
           maps.put("columns", getDataGridColumn(j));
           resultData.add(maps);
       }
        return resultData;
    }

    private List getDataGridColumn(JxAssessBzcjZg j) {
        List columns=new ArrayList();

        DataGridColumn dataGridColumn=null;
        String name1 = j.getName1();
        if (!StringUtils.isBlank(name1)){
            dataGridColumn=getDataGridColumn(1,name1);
            columns.add(dataGridColumn);
        }

        String name2 = j.getName2();
        if (!StringUtils.isBlank(name2)){
            dataGridColumn=getDataGridColumn(2,name2);
            columns.add(dataGridColumn);
        }
        String name3 = j.getName3();
        if (!StringUtils.isBlank(name3)){
            dataGridColumn=getDataGridColumn(3,name3);
            columns.add(dataGridColumn);
        }
        String name4 = j.getName4();
        if (!StringUtils.isBlank(name4)){
            dataGridColumn=getDataGridColumn(4,name4);
            columns.add(dataGridColumn);
        }
        String name5 = j.getName5();
        if (!StringUtils.isBlank(name5)){
            dataGridColumn=getDataGridColumn(5,name5);
            columns.add(dataGridColumn);
        }
        String name6 = j.getName6();
        if (!StringUtils.isBlank(name6)){
            dataGridColumn=getDataGridColumn(6,name6);
            columns.add(dataGridColumn);
        }
        String name7 = j.getName7();
        if (!StringUtils.isBlank(name7)){
            dataGridColumn=getDataGridColumn(7,name7);
            columns.add(dataGridColumn);
        }
        String name8 = j.getName8();
        if (!StringUtils.isBlank(name8)){
            dataGridColumn=getDataGridColumn(8,name8);
            columns.add(dataGridColumn);
        }
        String name9 = j.getName9();
        if (!StringUtils.isBlank(name9)){
            dataGridColumn=getDataGridColumn(9,name9);
            columns.add(dataGridColumn);
        }
        String name10 = j.getName10();
        if (!StringUtils.isBlank(name10)){
            dataGridColumn=getDataGridColumn(10,name10);
            columns.add(dataGridColumn);
        }
        String name11 = j.getName11();
        if (!StringUtils.isBlank(name11)){
            dataGridColumn=getDataGridColumn(11,name11);
            columns.add(dataGridColumn);
        }
        String name12 = j.getName12();
        if (!StringUtils.isBlank(name12)){
            dataGridColumn=getDataGridColumn(12,name12);
            columns.add(dataGridColumn);
        }

        return columns;
    }

    @Override
    public Object selectBzcjZhuGuanByEmpService(JxAssessReception jxAssessReception) {
        //获取必要查询的数据
        Integer year = jxAssessReception.getYear();
        Integer month = jxAssessReception.getMonth();
        String pkDept = jxAssessReception.getPkDept();
        String postName = jxAssessReception.getPostName();  //"产线主管,辅助主管"

        //定义变量封装数据集合
        List<Object> resultData= new ArrayList<>();

        //查询主管：从员工表中获取所有主管的信息(基本信息)：辅助主管、产线主管
        List<JxEmp> jxEmps = jxAssessCommonService.selectDeptAllZhuGuan(pkDept, Arrays.asList(postName.split(",")));

        for (JxEmp j:jxEmps){//
            List<JxAssessBzcjZg> data= new ArrayList<>();
            List<DataGridColumn> columns =new  ArrayList<>();
            Map<String,Object> map= new HashMap<>();
            //值封装：封装主管基本信息
            JxAssessBzcjZg jxAssessBzcjZg = new JxAssessBzcjZg();
            String empCode = j.getEmpCode();
            jxAssessBzcjZg.setEmpCode(empCode);  //主管工号
            jxAssessBzcjZg.setEmpName(j.getEmpName());  //主管姓名
            //获取主管的岗位名称
            String zgPostName = j.getPostName();
            jxAssessBzcjZg.setPostName(zgPostName);
            //依次查询每个主管下的组长员工信息
            List<JxEmp> zuZhangs=null;
            if (zgPostName.contains("辅助")){ //判断岗位名称是否有辅助
                //获取辅助组长的数据
                zuZhangs =jxEmpService.selectJxEmpByPkDeptAndPostIds(pkDept,empCode,14+""); //辅助组长岗位id是14
            }else {
                //获取产线组长的数据
                zuZhangs =jxEmpService.selectJxEmpByPkDeptAndPostIds(pkDept,empCode,11+"");
            }

            //根据获取到的组长去绩效中找数据:条件工号、年、月、部门
            int i=1; //组长名称字段
            for (JxEmp b:zuZhangs){
                //表字段封装：开始表字段的封装---------------------------------------------------------------------------
                columns.add(getDataGridColumn(i, b.getEmpName()));      //第一个组长的字段
                //开始值的封装------------------------------------------------
                getData(i,b,year, month, pkDept, jxAssessBzcjZg);
                i++;
            }
            data.add(jxAssessBzcjZg);
            map.put("columns", columns);
            map.put("data", data);

            resultData.add(map);
        };

        //  {
        //            "data":[ {empName: "方水珠",empCode:"0056825",postName:"产线主管",zuZhangName1:"800",zuZhangName2:"800",zuZhangName3:"800",zuZhangName4:"800",zuZhangName5:"800",
        //                performanceBonus:"",attendanceRate:"",exceptionBonus:"",illegalFine:"",resultMoney:"",remarks:"",assessType:"" }],
        //            "columns":[
        //                {"field": 'zuZhangName1', "title": '李松娥', "width": "5%","align": "center","editor": {"type":"textbox", "options": {"onChange":bzCxZhuGuanPerformanceBonus}}},
        //                {"field": 'zuZhangName2', "title": '杨顺连', "width": "5%","align": "center","editor": {"type":"textbox", "options": {"onChange":bzCxZhuGuanPerformanceBonus}}},
        //            ]
        //        },

        return resultData;
    }

    @Override
    @Transactional("mysqlTransactionManager")
    public Object saveZhuGuanAssessDataService(JxAssessReception jxAssessReception) {
        try {
            //主管数据
            List<JxAssessBzcjZg> jxAssessBzcjZgs = jxAssessReception.getJxAssessBzcjZgs();
            //清空数据
            jxAssessBzcjZgService.deleteByYearAndMonth(jxAssessBzcjZgs.get(0).getAssessYear(),jxAssessBzcjZgs.get(0).getAssessMonth());

            jxAssessBzcjZgs.forEach(jxAssessBzcjZg->{
                //添加数据
                Integer i= jxAssessBzcjZgService.insert(jxAssessBzcjZg);
            });
            //主管总合计金额数据
            JxAssessTotal jxAssessTotal = jxAssessReception.getJxAssessTotal();
            //先清空数据
            jxAssessBzcjTotalService.deleteByExample(jxAssessTotal);
            //插入数据
            jxAssessBzcjTotalService.insertSelective(jxAssessTotal);
        }catch (Exception e){
           throw new JxAssessException(WSC.REQUEST_ERROR, JxConst.SAVE_DEFEATED);
        }
        return new R(WSC.RESTPONSE_OK,JxConst.SUBMIT_SUCCESS);
    }

    private JxAssessBzcjZg getData(int i,JxEmp b,Integer year,Integer month,String pkDept, JxAssessBzcjZg jxAssessBzcjZg){
        //查询组长绩效表中的数据
            //辅助组长和产线组长不在同一个表中
        List jxAssessBzcjs=new ArrayList<>();
        String empCode = b.getEmpCode();
        String empName = b.getEmpName();
        if(b.getPostId()==11){
            JxAssessBzcjExample jxAssessBzcjExample = new JxAssessBzcjExample();
            jxAssessBzcjExample.createCriteria()
                    .andEmpCodeEqualTo(empCode)    //工号
                    .andAssessMonthEqualTo(month)
                    .andAssessYearEqualTo(year)
                    .andAssessDeptEqualTo(pkDept);
            //获取到组长绩效表中的信息
            jxAssessBzcjs = jxAssessBzcjMapper.selectByExample(jxAssessBzcjExample);
        }else {//辅助组长数据
            jxAssessBzcjs = jxAssessBzcjFzryMapper.selectBzcjFuZhuRenYuanByYearAndMonthAndEmpCode(year,month,empCode);
        }

        if (jxAssessBzcjs.size()>0){
            Object typeData=jxAssessBzcjs.get(0);
            //判断是哪个表类型
            double penaltyBonus=0;  //定义主管中组长绩效奖金最终值
            if (typeData instanceof JxAssessBzcj){
                //主管有可能有两条数据：产线1和产线2；1线取绩效奖金，2线取最终绩效奖金来计算;
                if (jxAssessBzcjs.size()>0){
                    JxAssessBzcj jxAssessBzcj = (JxAssessBzcj)typeData;
                    //只有一条线：看是2线还是1线
                    if (jxAssessBzcjs.size()==1){
                        penaltyBonus=  getEmpGropMoney(jxAssessBzcj);
                    }else {
                        //有两条线：1线的绩效奖金标准 加 2线的最终绩效奖金标准
                        JxAssessBzcj jxAssessBzcj1 = (JxAssessBzcj)jxAssessBzcjs.get(1);
                        double money=getEmpGropMoney(jxAssessBzcj);
                        double money1=getEmpGropMoney(jxAssessBzcj1);
                        penaltyBonus=money+money1;
                    }
                    pkcagePenaltyBonus(i,jxAssessBzcjZg,penaltyBonus,empName);
                }

            }else { //辅助组长只要最终绩效奖金的数据即可
                JxAssessBzcjFzry jxAssessBzcjFzry= (JxAssessBzcjFzry) typeData;
                penaltyBonus=Double.parseDouble(jxAssessBzcjFzry.getResultMoney());
                pkcagePenaltyBonus(i,jxAssessBzcjZg,penaltyBonus,empName);
            }
        }

        // {empName: "方水珠",empCode:"0056825",postName:"产线主管",zuZhangName1:"800",zuZhangName2:"800",zuZhangName3:"800",zuZhangName4:"800",zuZhangName5:"800",
        //        //                performanceBonus:"",attendanceRate:"",exceptionBonus:"",illegalFine:"",resultMoney:"",remarks:"",assessType:"" }
        return jxAssessBzcjZg;
    }

    private void pkcagePenaltyBonus(int i,JxAssessBzcjZg jxAssessBzcjZg,double penaltyBonus,String empName){
        switch (i){
            case 1:
                jxAssessBzcjZg.setValue1(penaltyBonus+""); //组长1的绩效金额
                jxAssessBzcjZg.setName1(empName);             //这里封装名字，后端进行保存数据到数据就很方便了
                break;
            case 2:
                jxAssessBzcjZg.setValue2(penaltyBonus+""); //组长2的绩效金额
                jxAssessBzcjZg.setName2(empName);
                break;
            case 3:
                jxAssessBzcjZg.setValue3(penaltyBonus+""); //组长3的绩效金额
                jxAssessBzcjZg.setName3(empName);
                break;
            case 4:
                jxAssessBzcjZg.setValue4(penaltyBonus+""); //组长4的绩效金额
                jxAssessBzcjZg.setName4(empName);
                break;
            case 5:
                jxAssessBzcjZg.setValue5(penaltyBonus+""); //组长5的绩效金额
                jxAssessBzcjZg.setName5(empName);
                break;
            case 6:
                jxAssessBzcjZg.setValue6(penaltyBonus+""); //组长6的绩效金额
                jxAssessBzcjZg.setName6(empName);
                break;
            case 7:
                jxAssessBzcjZg.setValue7(penaltyBonus+""); //组长7的绩效金额
                jxAssessBzcjZg.setName7(empName);
                break;
            case 8:
                jxAssessBzcjZg.setValue8(penaltyBonus+""); //组长8的绩效金额
                jxAssessBzcjZg.setName8(empName);
                break;
            case 9:
                jxAssessBzcjZg.setValue9(penaltyBonus+""); //组长9的绩效金额
                jxAssessBzcjZg.setName9(empName);
                break;
            case 10:
                jxAssessBzcjZg.setValue10(penaltyBonus+""); //组长10的绩效金额
                jxAssessBzcjZg.setName10(empName);
                break;
            case 11:
                jxAssessBzcjZg.setValue11(penaltyBonus+""); //组长11的绩效金额
                jxAssessBzcjZg.setName11(empName);
                break;
            case 12:
                jxAssessBzcjZg.setValue12(penaltyBonus+""); //组长12的绩效金额
                jxAssessBzcjZg.setName12(empName);
                break;
        }
    }

    private  DataGridColumn getDataGridColumn(int i,String titleValue){

        //生成主管的字段
        //"columns":[
        //                {"field": 'zuZhangName1', "title": '李松娥', "width": "5%","align": "center","editor": {"type":"textbox", "options": {"onChange":bzCxZhuGuanPerformanceBonus}}},
        //                {"field": 'zuZhangName2', "title": '杨顺连', "width": "5%","align": "center","editor": {"type":"textbox", "options": {"onChange":bzCxZhuGuanPerformanceBonus}}},
        //            ]
        DataGridColumn column =new DataGridColumn();
        column.setField("value"+i+"");
        column.setTitle(titleValue);
        column.setWidth("5%");
        column.setAlign("center");
        DataGridColumn.Edit edit= new DataGridColumn().new Edit();
        edit.setType("textbox");
        Map<String,Object> map = new HashedMap();
        map.put("onChange","bzCxZhuGuanPerformanceBonus");
        edit.setOptions(map);
        column.setEditor(edit);
        return column;
    }

    //判断是那条线的取哪个值
    private Double  getEmpGropMoney(JxAssessBzcj jxAssessBzcj) {
        double montey = 0;
        if (jxAssessBzcj !=null){

            if ("A".equals(jxAssessBzcj.getEmpGroup())){ //1线:我只要绩效奖金
                String money = jxAssessBzcj.getPerformanceBonus();  //绩效奖金
                if (money !=null){
                    montey= Double.parseDouble(money);
                }

            }else {  //2线：我只要最终绩效奖金
                String money1 = jxAssessBzcj.getResultMoney();  //最终绩效奖金
                if (money1 !=null){

                    montey = Double.parseDouble(money1);
                }
            }
        }
        return montey;
    }

    private JxAssessBzcjExample getJxAssessBzcjExample(JxAssessBzcj condition){
        if (condition !=null){
            JxAssessBzcjExample jxAssessBzcjExample= new JxAssessBzcjExample();
            JxAssessBzcjExample.Criteria criteria = jxAssessBzcjExample.createCriteria();
            String empCode = condition.getEmpCode();                //员工工号
            String empName = condition.getEmpName();                //员工姓名
            Integer assessYear = condition.getAssessYear();         //考核年份
            Integer assessMonth = condition.getAssessMonth();       //考核月份
            String postName = condition.getPostName();              //岗位名称
            String assessDept = condition.getAssessDept();          //部门
            String empGroup = condition.getEmpGroup();              //班组：A、B、C、D.......
            Integer assessType = condition.getAssessType();         //包装车间绩效数据类型
            if (!StringUtils.isBlank(empCode)){
                criteria.andEmpCodeEqualTo(empCode);
            }
            if (!StringUtils.isBlank(empName)){
                criteria.andEmpNameEqualTo(empName);
            }
            if (assessType !=null){
                criteria.andAssessTypeEqualTo(assessType);
            }
            if (assessYear !=null){
                criteria.andAssessYearEqualTo(assessYear);
            }
            if (assessMonth !=null){
                criteria.andAssessMonthEqualTo(assessMonth);
            }
            if (!StringUtils.isBlank(postName)){
                criteria.andPostNameEqualTo(postName);
            }
            if (!StringUtils.isBlank(assessDept)){
                criteria.andAssessDeptEqualTo(assessDept);
            }
            if (!StringUtils.isBlank(empGroup)){
                criteria.andEmpGroupEqualTo(empGroup);
            }

            return jxAssessBzcjExample;
        }
        return null;
    }

    //判断前端提交数据
    private void  isSubmitData(JxAssessReception jxAssessPzcjReception){
        //判断数据是否有效
        List<JxAssessBzcj> jxAssessBzcjs = jxAssessPzcjReception.getJxAssessBzcjs();
        if (jxAssessBzcjs.size()<=0){
            throw new JxAssessException(WSC.PRECONDITIONFAILED, JxConst.SUBMIT_EFEATED_NULL_DATA);
        }
    }


}
